<?php

/**
 * This is the model class for table "{{menu}}".
 *
 * The followings are the available columns in table '{{menu}}':
 * @property integer $id
 * @property string $name
 * @property string $description
 * @property integer $created_user_id
 * @property string $created_date
 * @property integer $update_user_id
 * @property string $update_date
 * @property string $language
 */
class Menu extends CActiveRecord
{
    public $created_date_range;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Menu the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{menu}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            array('name','required'),
			array('created_user_id, update_user_id', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>255),
			array('language', 'length', 'max'=>10),
			array('description, created_date, update_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('created_date_range, id, name, description, created_user_id, created_date, update_user_id, update_date, language', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => t('ID','menu'),
			'name' => t('Name','menu'),
			'description' => t('Description','menu'),
			'created_user_id' => t('Create User ID','menu'),
			'created_date' => t('Created Date','menu'),
			'update_user_id' => t('Update User','menu'),
			'update_date' => t('Update Date','menu'),
			'language' => t('Language','menu'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('created_user_id',$this->created_user_id);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('update_user_id',$this->update_user_id);
		$criteria->compare('update_date',$this->update_date,true);
		$criteria->compare('language',$this->language,true);

        if(isset($this->created_date_range) && trim($this->created_date_range) != '') {
            $created_date = explode(" - ",$this->created_date_range);
            $date_start = convertTimeZone(date('Y-m-d H:i:s',CDateTimeParser::parse($created_date[0],app()->locale->getDateFormat('short'))),app()->timeZone);
            $date_end = convertTimeZone(date('Y-m-d H:i:s',CDateTimeParser::parse($created_date[1],app()->locale->getDateFormat('short'))),app()->timeZone);
            $criteria->addBetweenCondition('created_date',$date_start,$date_end);
        }

        $dependency = new CDbCacheDependency("SELECT MAX(update_date),MAX(created_date) FROM {{menu}}");

		return new CActiveDataProvider(self::model()->cache(settings()->get('system','cache_time'),$dependency,2), array(
			'criteria'=>$criteria,
		));
	}
    public function beforeSave() {
        $current_user = getSession('current_user');
        if($this->getIsNewRecord()) {
            $this->created_date = new CDbExpression('UTC_TIMESTAMP()');
            $this->created_user_id = $current_user['id'];
        } else {
            $this->update_date = new CDbExpression('UTC_TIMESTAMP()');
            $this->update_user_id = $current_user['id'];
        }
        return parent::beforeSave();
    }
}